//app.js
App({
	onLaunch: function() {
		var that = this
		wx.getSystemInfo({
			success(res) {
				that.systemInfo = res;
			},
		});

		// 登录
		wx.login({
			success: res => {
				that.globalData.code = res.code;
				// 发送 res.code 到后台换取 openId, sessionKey, unionId
			}
		});
		// 获取用户信息
		wx.getSetting({
			success: res => {
				if(!res.authSetting['scope.userInfo']) {
					wx.authorize({
						scope: 'scope.userInfo',
						success(errMsg) {
							console.log(errMsg);
							// 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
							wx.getUserInfo({
								success: res => {
									console.log(res)
									// 可以将 res 发送给后台解码出 unionId
									that.globalData.userInfo = res.userInfo
									that.globalData.encryptedData =  res.encryptedData
									that.globalData.iv = res.iv
								}
							})
						},
						fail() {
							wx.openSetting({
								success: (res) => {
									if(res.authSetting["scope.userInfo"]){
										console.log(123);
									}
								}
							})
						}
					})
				}
			}
		})
	},
	getUserInfo: function(cb) {
		var that = this
		if(this.globalData.userInfo) {
			typeof cb == "function" && cb(this.globalData.userInfo)
		} else {
			wx.login({
				success: function() {
					wx.getUserInfo({
						success: function(res) {
							console.log(res)
							that.globalData.userInfo = res.userInfo
							that.globalData.encryptedData =  res.encryptedData
							that.globalData.iv = res.iv
							typeof cb == "function" && cb(that.globalData)
						}
					})
				}
			})
		}
	},
	getLocation:function(cb){
		var that = this
		if(this.globalData.latitude) {
			typeof cb == "function" && cb(this.globalData.latitude,this.globalData.longitude)
		} else {
			wx.getLocation({
				type: 'gcj02',
				success: function(res) {
					that.globalData.latitude = res.latitude;
					that.globalData.longitude = res.longitude;
					typeof cb == "function" && cb(that.globalData.latitude,that.globalData.longitude)
				}
			})
		}
	},
	globalData: {
		userInfo: null,
		code: null,
		encryptedData:null,
		iv:null,
		userId:"",
		latitude:"",
		longitude:""
	},
	systemInfo: null,
	search:{
		age:"",
		liudian:"",
		kctype:"",
		address:"",
	}
})